#pragma once
//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "VxDefs.h"
#include "vx_debug.h"

#ifdef WIN_PLATFORM
	#include "winsock2.h"
	#include "windows.h"
#else
	#include <pthread.h>
#endif // WIN_PLATFORM

//#define DEBUG_VX_MUTEX 1

#ifdef DEBUG_VX_MUTEX
#define MAX_MUTEX_THREADS 50
class VxMutexDebug
{
public:
	VxMutexDebug()
	{
		m_u32ThreadId = 0;
		m_pLastLockFile = "NOLOCKCALLED";
		m_iLastLockLine = 0;
		m_pLastUnlockFile = "NOUNLOCKCALLED";
		m_iLastUnlockLine = 0;
		m_iLockCnt = 0;
	}
	U32		m_u32ThreadId;
	char *	m_pLastLockFile;
	int		m_iLastLockLine;
	char *	m_pLastUnlockFile;
	int		m_iLastUnlockLine;
	int		m_iLockCnt;
};
#endif // DEBUG_VX_MUTEX

class VxMutex
{
public:
	//=== constructor ===//
	VxMutex();
	//=== destructor ===//
	virtual ~VxMutex();

	//=== implementation ===//
	int Lock( void );
	int Unlock( void );
	int Lock( int iInstance );
	int Unlock( int iInstance );
	int Lock( const char* file, int line );
	int Unlock( const char* file, int line );

#ifdef DEBUG_VX_MUTEX
	VxMutexDebug	m_agMutexThreadIds[ MAX_MUTEX_THREADS ];
	UINT			m_uiLastLockThreadId;
#endif //DEBUG_VX_MUTEX

	//=== vars ===//
	#ifdef WIN_PLATFORM
		HANDLE				m_hAccessLock;
	#else // LINUX
		pthread_mutex_t		m_oLock;
		pthread_mutexattr_t m_oMutexAttr;
	#endif
};




